<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 18-9-27
 * Time: 上午10:41
 */

namespace app\admin\controller;


use app\model\ActivityCollectionPoint;
use app\model\ActivityGroupStage;
use app\model\ActivityOrderAttach;
use extend\STATUS_CODE;
use think\Request;

class Activity extends Auth
{
    /**
     * 修改团期可报名状态
     * @param Request $request
     * @return array
     */
    public function changeGroupStageCanSignUpStatusById (Request $request)
    {
        $group_stage_id     = $request->post('id');
        $status = (int) $request->post('status');
        if (!in_array($status, [1,2])) {
            return self::response(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误'
            );
        }
        $result = ActivityGroupStage::M()
            ->where('id', $group_stage_id)
            ->update(['can_sign_up' => $status]);
        if ($result) {
            return self::response();
        }
        return self::response(
            STATUS_CODE::DELETE_FAIL,
            '修改失败'
        );
    }
    /**
     * @purpose 修改集合点报名状态
     * @param Request $request
     * @return array
     */
    public function changePointStatusById (Request $request)
    {
        $id     = $request->post('id');
        $status = (int) $request->post('status');
        if (!in_array($status, [1,2])) {
            return self::response(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误'
            );
        }
        $result = ActivityCollectionPoint::M()
            ->where(['id' => $id])
            ->update(['status' => $status]);
        if ($result) {
            return self::response();
        }
        return self::response(
            STATUS_CODE::DELETE_FAIL,
            '修改失败'
        );
    }
    /**
     * @purpose 删除集合点
     * @param Request $request
     * @throws \Exception
     */
    public function deleteCollectionPointById (Request $request)
    {
        $id     = $request->post('id');
        // 检测当前集合点下是否有人报名
        if (ActivityOrderAttach::M()->where([
                ['collection_point_id','=', $id],
                ['status', ['=', 3], ['=', 9], 'or']
            ])->count(1) > 0) {
            return self::response(
                STATUS_CODE::FAIL,
                '当前集合点下有人报名,不可进行删除操作'
            );
        }
        $result = ActivityCollectionPoint::M()
            ->where('id', $id)
            ->delete();
        if ($result) {
            return self::response();
        }
        return self::response(
            STATUS_CODE::DELETE_FAIL,
            '删除失败'
        );
    }
    /**
     * @purpose 集合点列表统计
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function collectionPointTravelByGroupStage (Request $request)
    {
        $group_stage_id =   $request->get('group_stage_id', 0);
        return \app\admin\logic\Activity::collectionPointTravelByGroupStage($group_stage_id);
    }
    /**
     * @purpose 获取活动报名记录
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function signUpRecord (Request $request)
    {
        $page       = $request->get('page', 1);
        $size       = $request->get('size', 20);
        $status     = $request->get('status', 1);
        return \app\admin\logic\Activity::signUpRecord($status, $page, $size);
    }
    /**
     * @purpose 批量设置上车点人数限制
     * @param Request $request
     * @return array
     * @throws \Exception
     */
    public function batchSetCollectionPointNumberOfPeople (Request $request)
    {
        $data       = $request->post();
        return \app\admin\logic\Activity::batchSetCollectionPointNumberOfPeople($data);
    }
    /**
     * @purpose 添加集合点
     * @param Request $request
     * @return array
     */
    public function createCollectionPoint (Request $request)
    {
        $data   = $request->post();
        return \app\admin\logic\Activity::createCollectionPoint($data);
    }
    /**
     * @purpose 编辑集合点
     * @param Request $request
     * @return array
     */
    public function editCollectionPoint (Request $request)
    {
        $data       = $request->post();
        return \app\admin\logic\Activity::editCollectionPoint($data);
    }
    /**
     * @purpose 获取团期集合点列表
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function collectionPointItems (Request $request) {
        $group_stage_id     = $request->get('group_stage_id', 0);
        return \app\admin\logic\Activity::collectionPointItems($group_stage_id);
    }

    /**
     * @purpose 获取俱乐部(为活动发布提供俱乐部选择)
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function activityClubItems (Request $request)
    {
        return \app\admin\logic\Activity::activityClubItems();
    }

    /**
     * @purpose 发布活动
     * @param Request $request
     * @return array
     * @throws \think\exception\PDOException
     */
    public function add (Request $request)
    {
        $data = $request->post();
        return \app\admin\logic\Activity::add($data);
    }

    /**
     * @purpose 通过俱乐部ID获取活动列表(可通过区域选择显示活动)
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function activityItems (Request $request)
    {
        $page       = $request->get('page', 1);
        $size       = $request->get('size', 20);
        $kw         = $request->get('kw', '');
        $status     = $request->get('status', '');
        $where = [];
        if ($kw) {
            $where[] = ['a.title', ['like', $kw.'%'], ['like', '%'.$kw.'%'], ['like', '%'.$kw], 'or'];
        }
        if ($status == 1) {
            $where[] = ['gs.status', '=', 1];
            $where[] = ['gs.end_time', '>', time()];
        } else if ($status == 99) {
            $where[] = ['gs.status', '=', 99];
            $where[] = ['gs.start_time', '>', time()];
        } else if ($status == 'previous') {
            $where[] = ['gs.end_time', '<', time()];
        } else if ($status == 'traveling') {
            $where[] = ['gs.status', '=', 99];
            $where[] = ['gs.start_time', '<', time()];
            $where[] = ['gs.end_time', '>', time()];
        }
        return \app\admin\logic\Activity::activityItems(
            $where,
            $page,
            $size
        );
    }

    /**
     * @purpose 获取单条活动数据(用于修改本条活动信息)
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function activitySingleData (Request $request)
    {
        $id = $request->get('activity_id',0);
        return \app\admin\logic\Activity::activitySingleData($id);
    }

    /**
     * @purpose 通过活动ID修改活动信息
     * @param Request $request
     * @return array
     */
    public function activityEdit (Request $request)
    {
        $data  =  $request->post();
        return \app\admin\logic\Activity::activityEdit($data);
    }

    /**
     * @purpose 更改活动状态
     * @param Request $request
     * @return array
     */
    public function editUpperAndLower (Request $request)
    {
        $data = $request->post();
        return \app\admin\logic\Activity::editUpperAndLower($data);
    }

    /**
     * @purpose 获取单条团期数据
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function groupStageSingleData (Request $request)
    {
        $id = $request->get('group_stage_id',-1);
        return \app\admin\logic\Activity::groupStageSingleData($id);
    }

    /**
     * @purpose 通过团期ID修改团期信息
     * @param Request $request
     * @return array
     */
    public function groupStageEdit (Request $request)
    {
        $data  =  $request->post();
        return \app\admin\logic\Activity::groupStageEdit($data);
    }
}